/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  VirtAcctPreAuth
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月12日
 *
 */
package cc.rengu.igas.acct.core.service.trans;

import cc.rengu.igas.acct.common.entity.AcctDetail;
import cc.rengu.igas.acct.common.enums.AcctTransStatusEnum;
import cc.rengu.igas.acct.core.service.realize.AcctOpt;
import cc.rengu.igas.acct.facade.request.VirtAcctPreAuthCancelRequest;
import cc.rengu.igas.acct.facade.response.VirtAcctPreAuthCancelResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSONObject;

/**
 * 预授权撤销
 * 原有冻结的金额转到可用余额，整体账户余额不变
 *
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/8
 */
public class VirtAcctPreAuthCancelService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        try {
            VirtAcctPreAuthCancelRequest virtAcctPreAuthCancelRequest = new VirtAcctPreAuthCancelRequest();
            ConvertUtil.convertOutput(virtAcctPreAuthCancelRequest);
            rglog.info("virtAcctPreAuthCancelRequest = <{}>", JSONObject.toJSONString(virtAcctPreAuthCancelRequest));
            VirtAcctPreAuthCancelResponse virtAcctPreAuthCancelResponse = new VirtAcctPreAuthCancelResponse();
            virtAcctPreAuthCancelResponse.setHeader(virtAcctPreAuthCancelRequest.getHeader());
            BizResponse<VirtAcctPreAuthCancelResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(virtAcctPreAuthCancelResponse);
            //冻结撤销操作
            AcctDetail acctDetail = new AcctOpt().freezeCancel(virtAcctPreAuthCancelRequest.getHeader().getTransDate(),
                    virtAcctPreAuthCancelRequest.getHeader().getTransTime(), virtAcctPreAuthCancelRequest.getHeader().getTxnNum(),
                    virtAcctPreAuthCancelRequest.getHeader().getSrcSysId(), virtAcctPreAuthCancelRequest.getHeader().getInstId(),
                    virtAcctPreAuthCancelRequest.getHeader().getTraceNo(), virtAcctPreAuthCancelRequest.getFreezeAcctSeqNum(),
                    virtAcctPreAuthCancelRequest.getTransAmt(),
                    virtAcctPreAuthCancelRequest.getAcctNo(), virtAcctPreAuthCancelRequest.getCustomerId(),
                    virtAcctPreAuthCancelRequest.getUserType(), virtAcctPreAuthCancelRequest.getUserId(),
                    virtAcctPreAuthCancelRequest.getSummary(), rglog);

            virtAcctPreAuthCancelResponse.setAfterBalance(acctDetail.getAfterBalance());
            virtAcctPreAuthCancelResponse.setAfterAvailableBalance(acctDetail.getAfterAvailableBalance());
            virtAcctPreAuthCancelResponse.setAfterFreezeBalance(acctDetail.getAfterFreezeBalance());
            virtAcctPreAuthCancelResponse.setAcctDate(acctDetail.getAcctDate());
            virtAcctPreAuthCancelResponse.setAcctSeqNum(acctDetail.getAcctSeqNum());
            virtAcctPreAuthCancelResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            virtAcctPreAuthCancelResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            virtAcctPreAuthCancelResponse.setTransStatus(AcctTransStatusEnum.SUCC.getStatus());
            bizResponse.setRspSysId(ChannelEnum.ACCT.getChannelType());
            bizResponse.setTransStatus(TransStatusEnum.TRANS_SUCC);
            bizResponse.setRespCode(OltpRpcdEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.TRANS_SUCCESS.getRespDesc());
            ConvertUtil.convertInput(bizResponse.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE,
                        OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC,
                        OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }
        return 0;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }
}
